package org.platform.tool.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.UUID;
import java.util.regex.Pattern;

public class ComUtils {

    /**
     * 判断是否是正整数
     *
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    /**
     * 字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isNullOrEmpty(String str) {
        return null == str || str.trim().equals("");
    }

    /**
     * 字符串数组是否包含指定字符串
     *
     * @param aStr
     * @param str
     * @return
     */
    public static boolean contains(String[] aStr, String str) {
        for (String item : aStr) {
            if (item.equals(str)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 字符串数组转换为字符串
     *
     * @param array
     * @return
     */
    public static String arrayConvertString(String[] array) {
        StringBuilder sb = new StringBuilder();
        for (String str : array) {
            sb.append(str).append(";");
        }
        return sb.toString();
    }

    /**
     * @param
     * @return
     */
    // public static boolean IsEmpty(Collection list){
    // return null == list || list.size() == 0;
    // }
    public static <T> boolean isArrayEmpty(T[] a) {
        return null == a || a.length <= 0;
    }

    @SuppressWarnings("rawtypes")
    public static boolean isEmpty(final Collection list) {
        return null == list || list.size() == 0;
    }

    /**
     * 格式化字符串
     *
     * @param msg
     * @param regex
     * @return
     */
    public static String convertFormatString(String msg, String regex) {
        String[] arr = msg.split(regex);
        StringBuilder sb = new StringBuilder();
        for (String str : arr) {
            sb.append("                            ").append(str).append("\r\n");
        }

        return sb.toString();
    }

    /**
     * @return
     */
    public static String getUUID() {
        String str = UUID.randomUUID().toString();
        str = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23)
                + str.substring(24);
        return str;
    }

    /**
     * 返回UUID的128位最显著64位值
     *
     * @return
     */
    public static long getLongUUID() {
        return UUID.randomUUID().getMostSignificantBits();
    }

    /**
     * long转字符串
     *
     * @param l
     * @return
     */
    public static String longToString(Long l) {
        if (null == l) {
            return "";
        } else {
            return l.toString();
        }
    }

    public static Long strToLong(String s) {
        return s == null ? null : Long.parseLong(s);
    }

    public static Float strToFloat(String s) {
        return s == null ? null : Float.parseFloat(s);
    }

    /**
     * Long 转 Integer
     */
    public static Integer longToInteger(Long l) {
        return l == null ? null : new Long(l).intValue();
    }

    /**
     * Integer转字符串
     */
    public static String integerToStr(Integer i) {
        return i != null ? i.toString() : "";
    }

    /**
     * toString
     */
    public static String objectToString(Object o) {
        return o == null ? "" : o.toString();
    }

    public static Integer intToInteger(int i) {
        return Integer.parseInt(objectToString(i));
    }

    public static Long intToLong(int i) {
        return Long.parseLong(objectToString(i));
    }

    public static Double strToDouble(String s) {
        return StringUtils.isEmpty(s) ? null : Double.parseDouble(s);
    }

    public static int strToInteger(String s) {
        return s.isEmpty() ? 0 : Integer.parseInt(s);
    }
}
